﻿#include "requestlist.h"
#include "ui_requestlist.h"

requestList::requestList(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::requestList)
{    
    ui->setupUi(this);
    fillStuff();
    showAll=false;

    dbModel = new myDataModel(this,ui->table,getQuery());

    dbModel->setHeader(QStringList()<<"№"<<"Дата"<<"Клиент"<<"Причина"<<"Результат"<<"Выполнено"<<"Примечание"<<"Сотрудник");

    ui->table->show();
    ui->table->setColumnHidden( RDB_attention, true);
    ui->table->scrollTo(dbModel->index(dbModel->rowCount()-1,0));


    connect(ui->table->selectionModel(), SIGNAL(currentRowChanged ( const QModelIndex &, const QModelIndex &)),
            this, SLOT(rcurrentRowChanged( const QModelIndex &, const QModelIndex &)));

    ui->table->horizontalHeader()->setResizeMode(RDB_enumber,QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(RDB_indate,QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(RDB_outdate,QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(RDB_who,QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(RDB_client,QHeaderView::Stretch);
    ui->table->horizontalHeader()->setResizeMode(RDB_problem,QHeaderView::Stretch);
    ui->table->horizontalHeader()->setResizeMode(RDB_result,QHeaderView::Stretch);
    ui->table->horizontalHeader()->setResizeMode(RDB_comment,QHeaderView::Stretch);


}

requestList::~requestList()
{
    delete ui;
    delete dbModel;
}

void requestList::updateList()
{
    dbModel->setQuery(getQuery());

}

void requestList::fillStuff()
{
    QSqlQuery query("SELECT * FROM stafflist ORDER by who DESC");
    while(query.next())ui->whoSelect->insertItem(0,query.value(0).toString());

}

QString requestList::getQuery()
{    return showAll?\
                "SELECT * FROM requestlist;":\
                "SELECT * FROM requestlist WHERE outdate is null;";
}


void requestList::rcurrentRowChanged(const QModelIndex &current, const QModelIndex &previous)
{
    ui->clientEdit->setText(dbModel->getData(RDB_client));
    ui->problemEdit->setText(dbModel->getData(RDB_problem));
    ui->resultEdit->setText(dbModel->getData(RDB_result));
    ui->commentEdit->setText(dbModel->getData(RDB_comment));
    ui->whoSelect->setEditText(dbModel->getData(RDB_who));
}

void requestList::on_showallButton_clicked()
{
    showAll=!showAll;
    dbModel->setQuery(getQuery());
    ui->showallButton->setText(showAll?"Текущее":"Все");
}

void requestList::on_searchButton_clicked()
{
    dbModel->setFilterFixedString(ui->searchEdit->text());
}

void requestList::on_searchEdit_returnPressed()
{
    dbModel->setFilterFixedString(ui->searchEdit->text());
}

void requestList::on_delsearchButton_clicked()
{
    ui->searchEdit->clear();
    dbModel->setFilterFixedString("");
}

void requestList::on_doneButton_clicked()
{
    if(ui->table->currentIndex().row()<0)return;
    QSqlQuery query;
    query.prepare("UPDATE requestlist SET outdate=:outdate WHERE enumber=:number");
    query.bindValue(":outdate",dbModel->getData(RDB_outdate).isEmpty()?QDate::currentDate():QDate::QDate());
    query.bindValue(":number",dbModel->getData(RDB_enumber));
    query.exec();

    if(query.lastError().isValid())QMessageBox::warning(0,"Неверный запрос",query.lastError().text());

    dbModel->setQuery(getQuery());
}

void requestList::on_addButton_clicked()
{
    if(ui->clientEdit->text().isEmpty()){
        QMessageBox::warning(0,"Ошибка","Введите клиента.");
        return;
    }
    QSqlQuery query;
    query.prepare("INSERT INTO requestlist "
                  "VALUES (DEFAULT,:indate,:client,:problem,:result,NULL,:comment,:who)");
    query.bindValue(":indate",QDate::currentDate());
    query.bindValue(":client",ui->clientEdit->text());
    query.bindValue(":problem",ui->problemEdit->text());
    query.bindValue(":result",ui->resultEdit->text());
    query.bindValue(":comment",ui->commentEdit->text());
    query.bindValue(":who",ui->whoSelect->currentText());
    query.exec();

    if(query.lastError().isValid())QMessageBox::warning(0,"Неверный запрос",query.lastError().text());

    dbModel->itemAdded();

}

void requestList::on_changeButton_clicked()
{
    if(ui->clientEdit->text().isEmpty()){
        QMessageBox::warning(0,"Ошибка","Введите клиента.");
        return;
    }

    QSqlQuery query;
    query.prepare("UPDATE requestlist "
                  "SET problem=:problem, result=:result, client=:client, comment=:comment, who=:who "
                  "WHERE enumber=:number");

    query.bindValue(":client",ui->clientEdit->text());
    query.bindValue(":problem",ui->problemEdit->text());
    query.bindValue(":result",ui->resultEdit->text());
    query.bindValue(":comment",ui->commentEdit->text());
    query.bindValue(":who",ui->whoSelect->currentText());
    query.bindValue(":number",dbModel->getData(RDB_enumber));
    query.exec();

    if(query.lastError().isValid())QMessageBox::warning(0,"Неверный запрос",query.lastError().text());

    dbModel->itemChanged();
}

